home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / python2.6 / compileall.py < prev    next >
Encoding:
Python Source  |  2010-12-26  |  6.8 KB  |  211 lines

  1. """Module/script to "compile" all .py files to .pyc (or .pyo) file.
  2.  
  3. When called as a script with arguments, this compiles the directories
  4. given as arguments recursively; the -l option prevents it from
  5. recursing into directories.
  6.  
  7. Without arguments, if compiles all modules on sys.path, without
  8. recursing into subdirectories.  (Even though it should do so for
  9. packages -- for now, you'll have to deal with packages separately.)
  10.  
  11. See module py_compile for details of the actual byte-compilation.
  12.  
  13. """
  14.  
  15. import os
  16. import sys
  17. import py_compile
  18.  
  19. __all__ = ["compile_dir","compile_file","compile_path"]
  20.  
  21. def compile_dir(dir, maxlevels=10, ddir=None,
  22.                 force=0, rx=None, quiet=0):
  23.     """Byte-compile all modules in the given directory tree.
  24.  
  25.     Arguments (only dir is required):
  26.  
  27.     dir:       the directory to byte-compile
  28.     maxlevels: maximum recursion level (default 10)
  29.     ddir:      if given, purported directory name (this is the
  30.                directory name that will show up in error messages)
  31.     force:     if 1, force compilation, even if timestamps are up-to-date
  32.     quiet:     if 1, be quiet during compilation
  33.  
  34.     """
  35.     if not quiet:
  36.         print 'Listing', dir, '...'
  37.     try:
  38.         names = os.listdir(dir)
  39.     except os.error:
  40.         print "Can't list", dir
  41.         names = []
  42.     names.sort()
  43.     success = 1
  44.     for name in names:
  45.         fullname = os.path.join(dir, name)
  46.         if ddir is not None:
  47.             dfile = os.path.join(ddir, name)
  48.         else:
  49.             dfile = None
  50.         if not os.path.isdir(fullname):
  51.             if not compile_file(fullname, ddir, force, rx, quiet):
  52.                 success = 0
  53.         elif maxlevels > 0 and \
  54.              name != os.curdir and name != os.pardir and \
  55.              os.path.isdir(fullname) and \
  56.              not os.path.islink(fullname):
  57.             if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet):
  58.                 success = 0
  59.     return success
  60.  
  61. def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
  62.     """Byte-compile file.
  63.     file:      the file to byte-compile
  64.     ddir:      if given, purported directory name (this is the
  65.                directory name that will show up in error messages)
  66.     force:     if 1, force compilation, even if timestamps are up-to-date
  67.     quiet:     if 1, be quiet during compilation
  68.  
  69.     """
  70.  
  71.     success = 1
  72.     name = os.path.basename(fullname)
  73.     if ddir is not None:
  74.         dfile = os.path.join(ddir, name)
  75.     else:
  76.         dfile = None
  77.     if rx is not None:
  78.         mo = rx.search(fullname)
  79.         if mo:
  80.             return success
  81.     if os.path.isfile(fullname):
  82.         head, tail = name[:-3], name[-3:]
  83.         if tail == '.py':
  84.             cfile = fullname + (__debug__ and 'c' or 'o')
  85.             ftime = os.stat(fullname).st_mtime
  86.             try: ctime = os.stat(cfile).st_mtime
  87.             except os.error: ctime = 0
  88.             if (ctime > ftime) and not force: return success
  89.             if not quiet:
  90.                 print 'Compiling', fullname, '...'
  91.             try:
  92.                 ok = py_compile.compile(fullname, None, dfile, True)
  93.             except KeyboardInterrupt:
  94.                 raise KeyboardInterrupt
  95.             except py_compile.PyCompileError,err:
  96.                 if quiet:
  97.                     print 'Compiling', fullname, '...'
  98.                 print err.msg
  99.                 success = 0
  100.             except IOError, e:
  101.                 print "Sorry", e
  102.                 success = 0
  103.             else:
  104.                 if ok == 0:
  105.                     success = 0
  106.     return success
  107.  
  108. def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
  109.     """Byte-compile all module on sys.path.
  110.  
  111.     Arguments (all optional):
  112.  
  113.     skip_curdir: if true, skip current directory (default true)
  114.     maxlevels:   max recursion level (default 0)
  115.     force: as for compile_dir() (default 0)
  116.     quiet: as for compile_dir() (default 0)
  117.  
  118.     """
  119.     success = 1
  120.     for dir in sys.path:
  121.         if (not dir or dir == os.curdir) and skip_curdir:
  122.             print 'Skipping current directory'
  123.         else:
  124.             success = success and compile_dir(dir, maxlevels, None,
  125.                                               force, quiet=quiet)
  126.     return success
  127.  
  128. def expand_args(args, flist):
  129.     """read names in flist and append to args"""
  130.     expanded = args[:]
  131.     if flist:
  132.         try:
  133.             if flist == '-':
  134.                 fd = sys.stdin
  135.             else:
  136.                 fd = open(flist)
  137.             while 1:
  138.                 line = fd.readline()
  139.                 if not line:
  140.                     break
  141.                 expanded.append(line[:-1])
  142.         except IOError:
  143.             print "Error reading file list %s" % flist
  144.             raise
  145.     return expanded
  146.  
  147. def main():
  148.     """Script main program."""
  149.     import getopt
  150.     try:
  151.         opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:')
  152.     except getopt.error, msg:
  153.         print msg
  154.         print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
  155.               "[-x regexp] [-i list] [directory|file ...]"
  156.         print "-l: don't recurse down"
  157.         print "-f: force rebuild even if timestamps are up-to-date"
  158.         print "-q: quiet operation"
  159.         print "-d destdir: purported directory name for error messages"
  160.         print "   if no directory arguments, -l sys.path is assumed"
  161.         print "-x regexp: skip files matching the regular expression regexp"
  162.         print "   the regexp is searched for in the full path of the file"
  163.         print "-i list: expand list with its content (file and directory names)"
  164.         sys.exit(2)
  165.     maxlevels = 10
  166.     ddir = None
  167.     force = 0
  168.     quiet = 0
  169.     rx = None
  170.     flist = None
  171.     for o, a in opts:
  172.         if o == '-l': maxlevels = 0
  173.         if o == '-d': ddir = a
  174.         if o == '-f': force = 1
  175.         if o == '-q': quiet = 1
  176.         if o == '-x':
  177.             import re
  178.             rx = re.compile(a)
  179.         if o == '-i': flist = a
  180.     if ddir:
  181.         if len(args) != 1 and not os.path.isdir(args[0]):
  182.             print "-d destdir require exactly one directory argument"
  183.             sys.exit(2)
  184.     success = 1
  185.     try:
  186.         if args or flist:
  187.             try:
  188.                 if flist:
  189.                     args = expand_args(args, flist)
  190.             except IOError:
  191.                 success = 0
  192.             if success:
  193.                 for arg in args:
  194.                     if os.path.isdir(arg):
  195.                         if not compile_dir(arg, maxlevels, ddir,
  196.                                            force, rx, quiet):
  197.                             success = 0
  198.                     else:
  199.                         if not compile_file(arg, ddir, force, rx, quiet):
  200.                             success = 0
  201.         else:
  202.             success = compile_path()
  203.     except KeyboardInterrupt:
  204.         print "\n[interrupt]"
  205.         success = 0
  206.     return success
  207.  
  208. if __name__ == '__main__':
  209.     exit_status = int(not main())
  210.     sys.exit(exit_status)
  211.